package com.max.common;

import org.springframework.http.HttpStatus;

public class R<T> {

    private T data;

    private String msg;

    private int code;

    public static <E> R<E> ok(E data) {
        R<E> r = new R<>();
        r.setCode(HttpStatus.OK.value());
        r.setMsg("成功");
        r.setData(data);
        return r;
    }

    public static R<?> result(int rows) {
        return result(rows > 0);
    }

    public static R<?> result(boolean success) {
        return success ? ok(null) : error();
    }


    public static R<?> error(int code, String msg) {
        R<?> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static R<?> error() {
        return error("失败");
    }

    public static R<?> error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
